package yunjiao.javatutorials.designpatterns.builder;

/**
 * TODO
 *
 * @author yangyunjiao
 */
public class PersonBuilder {
    private final Person person = new Person();

    public ValidPersonBuilder withFirstName(String firstName) {
        person.setFirstName(firstName);
        return new ValidPersonBuilder(person);
    }

    public InvalidPersonBuilder withLastName(String lastName) {
        person.setLastName(lastName);
        return new InvalidPersonBuilder(person);
    }

    public InvalidPersonBuilder withTitle(String title) {
        person.setTitle(title);
        return new InvalidPersonBuilder(person);
    }

    /**
     * 没有build方法
     */
    public static class InvalidPersonBuilder {
        private final Person person;

        public InvalidPersonBuilder(Person person) {
            this.person = person;
        }

        public InvalidPersonBuilder withLastName(String lastName) {
            person.setLastName(lastName);
            return this;
        }

        public InvalidPersonBuilder withTitle(String title) {
            person.setTitle(title);
            return this;
        }

        public ValidPersonBuilder withFirstName(String firstName) {
            person.setFirstName(firstName);
            return new ValidPersonBuilder(person);
        }
    }

    /**
     * 有build方法
     */
    public static class ValidPersonBuilder {
        private final Person person;

        public ValidPersonBuilder(Person person) {
            this.person = person;
        }

        public ValidPersonBuilder withLastName(String lastName) {
            person.setLastName(lastName);
            return this;
        }

        public ValidPersonBuilder withTitle(String title) {
            person.setTitle(title);
            return this;
        }

        public Person build() {
            return person;
        }
    }
}
